package com.heny.demo.strategy.impl;

import com.heny.demo.entity.TurnPoint;
import com.heny.demo.strategy.FileParseStrategy;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


/**
 * @author :wushen
 * @since :2025/5/11 16:44
 */

@Component("txt")
public class TxtFileParseStrategy implements FileParseStrategy {

    private static final GeometryFactory geometryFactory = new GeometryFactory();

    @Override
    public List<TurnPoint> parseFile(MultipartFile file) throws IOException {
        List<TurnPoint> turnPoints = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] data = line.trim().split("\\s+");
                if (data.length >= 4) {
                    TurnPoint turnPoint = new TurnPoint();
                    turnPoint.setIndex(String.valueOf(data[0]));
                    double x = Double.parseDouble(data[1]);
                    double y = Double.parseDouble(data[2]);
                    double z = Double.parseDouble(data[3]);
                    
                    Point point = geometryFactory.createPoint(new Coordinate(x, y));
                    turnPoint.setCoordinate(point);
//                    turnPoint.setZ(z);
                    
                    turnPoints.add(turnPoint);
                }
            }
        }
        return turnPoints;
    }
}    